<template>
  <div class="login-container">
    <el-form class="myform" :rules="rules" :model="form" ref="form">
      <h3>IHRM后台登陆系统</h3>
      <el-form-item prop="mobile">
        <el-input
          placeholder="请输入用户名"
          prefix-icon="el-icon-user-solid"
          v-model="form.mobile"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          placeholder="请输入密码"
          prefix-icon="el-icon-lock"
          show-password
          v-model="form.password"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="btn" @click="login" :loading="loading"
          >登录</el-button
        >
      </el-form-item>
      <a href="javascript:">还没账号?去注册</a>
    </el-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  data () {
    return {
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      loading: false,
      rules: {
        mobile: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value)
                ? callback()
                : callback(new Error('手机格式不正确'))
            },
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ...mapActions({
      myLogin: 'user/login'
    }),
    login () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.loading = true
          try {
            await this.myLogin(this.form)
            const _redirect = this.$route.query._redirect
            if(_redirect){
              this.$router.push(_redirect)
            }else{
              this.$router.push('/')
            }
            // let res = await loginApi(this.form)
            // console.log(res);
            this.$message.success('登录成功!')
          } catch (error) {
            console.log(error);
          }finally{
            this.loading = false            
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login-container {
  height: 100%;
  background-image: url('../../assets/common/login.jpg');
  background-position: center;
  .myform {
    padding: 35px 35px 15px 35px;
    width: 500px;
    height: 520px;
    position: fixed;
    top: 50%;
    left: 50%;
    text-align: center;
    transform: translate(-50%, -50%);
    input.el-input__inner {
      height: 45px;
    }
    h3 {
      color: #fff;
      font-size: 40px;
      font-weight: 100;
      letter-spacing: 8px;
      text-align: center;
    }
    .btn {
      width: 100%;
      background-color: #407ffe;
      color: #fff;
      height: 55px;
      border: none;
      font-size: 20px;
    }
    a {
      text-decoration: none;
      color: #fff;
    }
    .el-form-item__error {
      color: #fff;
    }
  }
}
</style>
